package com.example.smartplay.controller;


import com.example.smartplay.entity.Activity;
import com.example.smartplay.entity.Club;
import com.example.smartplay.entity.Result;
import com.example.smartplay.mapper.ActivityMapper;
import com.example.smartplay.service.ActivityService;
import com.example.smartplay.service.ActivityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2024-07-11
 */
@Slf4j
@RestController
@RequestMapping("/activity")
public class ActivityController {
    @Autowired
    private ActivityService activityService;
    @Autowired
    private ActivityMapper activityMapper;

    @GetMapping
    public Result list(){
        //调用service查询活动数据
        List<Activity> activitylist = activityService.list();
        return Result.success(activitylist);

    }

    //新增活动

    @PostMapping
    public Result add(@RequestBody Activity activity){
        log.info("新增活动:{}",activity);
        //调用service
        activityService.add(activity);
        return Result.success();
    }
    //删除活动
    @DeleteMapping("{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("根据ID删除活动:{}",ids);

        //调用service
        activityService.delete(ids);

        return Result.success();
    }
    //查询
    @GetMapping("/{id}")
    public Result find( @PathVariable("id") Integer id){
        Activity activity =  activityService.find(id);
        return Result.success(activity);
    }
    //查询（回显）后修改
    @PutMapping
    public Result update2(@RequestBody Activity activity){
        activityService.update2(activity);
        return Result.success();
    }

   /* //分页查询活动
    @GetMapping("/getactivity/{pageNum}/{pageSize}")
    public Result getactivity(
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize
    ){
        return activityService.getactivity(pageNum,pageSize);
    }*/
   @GetMapping("/getactivities/{pageNum}/{pageSize}")
   public Result getactivities(
           @RequestParam(value = "clubId",required = false) Integer clubId,
           @PathVariable("pageNum") Integer pageNum,
           @PathVariable("pageSize") Integer pageSize
   )
   {

       System.out.println("pageNum = " + pageNum);
       System.out.println("pageSize = " + pageSize);
       // 查询总记录数
       int total = activityMapper.countActivities();

       // 查询分页数据
       List<Activity> activities = activityMapper.getActivities (clubId,pageNum, pageSize);

       // 构建返回结果，包括分页数据和总记录数
       Map<String, Object> result = new HashMap<>();
       result.put("total", total);
       result.put("data", activities);

       return Result.success(result);
   }
}
